﻿#include <grpcpp/grpcpp.h>
#include "user.grpc.pb.h"
#include <iostream>

using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using user::UserRequest;
using user::UserResponse;
using user::UserService;

class UserClient {
public:
    UserClient(std::shared_ptr<Channel> channel) : stub_(UserService::NewStub(channel)) {}

    void GetUser(int id) {
        UserRequest request;
        request.set_id(id);

        UserResponse response;
        ClientContext context;

        Status status = stub_->GetUser(&context, request, &response);

        if (status.ok()) {
            std::cout << "User: " << response.name()
                      << ", Age: " << response.age() << std::endl;
        } else {
            std::cout << "RPC failed: " << status.error_message() << std::endl;
        }
    }

private:
    std::unique_ptr<UserService::Stub> stub_;
};

int main() {
    UserClient client(grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()));
    client.GetUser(1);
    return 0;
}
